package com.alibaba.citrus.ots.platform.enhance.functions.organization;

import com.alibaba.citrus.ots.common.exception.FunctionException;
import com.alibaba.citrus.ots.common.util.SpringContextUtil;
import com.alibaba.citrus.ots.platform.enhance.repository.OrganizationRelationRepository;
import com.alibaba.citrus.ots.platform.enhance.repository.OrganizationRepository;
import com.alibaba.citrus.ots.platform.enhance.repository.model.DubboResult;
import com.alibaba.cz.base.tool.message.ErrorMessage;
import com.alibaba.cz.base.tool.page.PageResult;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.otsplatformenhance.dto.request.IdQueryRequest;
import com.epoch.app.otsplatformenhance.enums.CommOperateTypeEnum;
import com.epoch.app.otsplatformenhance.enums.OrganizationTypeEnum;
import com.epoch.app.otsplatformenhance.sdo.OrganizationRelationSDO;
import com.epoch.app.otsplatformenhance.sdo.OrganizationSDO;
import lombok.Getter;
import lombok.Setter;

import java.util.List;

/**
 * @author zhoubc
 * @description: TODO
 * @date 2021/9/3 20:05
 */
public class OrganizationEntity {

    @Getter
    @Setter
    private OrganizationSDO sdo;

    @Getter
    @Setter
    private OrganizationRelationSDO relationSdo;

    private static OrganizationRepository organizationRepository;

    private static OrganizationRelationRepository organizationRelationRepository;

    static {
        organizationRepository = SpringContextUtil.getBean(OrganizationRepository.class);
        organizationRelationRepository = SpringContextUtil.getBean(OrganizationRelationRepository.class);
    }

    public static OrganizationEntity of(OrganizationSDO sdo,OrganizationRelationSDO relationSdo) {
        OrganizationEntity entity = new OrganizationEntity();
        entity.setSdo(sdo);
        entity.setRelationSdo(relationSdo);
        return entity;
    }

    public Result<String> doCreate() {
        businessCheck(sdo);
        return organizationRepository.createOrganization(sdo);
    }

    public Result<Boolean> doEdit() {
        businessCheck(sdo);
        return organizationRepository.editOrganization(sdo);
    }

    private Boolean businessCheck(OrganizationSDO sdo){
        //type 100-102 biz-1-PLATFORM    103-106 biz-10-COMPANY
        Integer type = sdo.getType();
        String biztype = sdo.getBizType();
        if(OrganizationTypeEnum.GROUP.getCode() == type
                || OrganizationTypeEnum.ENTERPRISE_GROUP.getCode() == type
                || OrganizationTypeEnum.REGION.getCode() == type){
            if(!"PLATFORM".equals(biztype.toUpperCase())){
                throw new FunctionException(ErrorMessage.code("OTS-01-003-01-15-011"));
            }
        }else if(OrganizationTypeEnum.COMPANY.getCode() == type
                || OrganizationTypeEnum.SALES_ORGANIZATION.getCode() == type
                || OrganizationTypeEnum.SALES_CHANNEL.getCode() == type
                || OrganizationTypeEnum.SALES_DEPARTMENT.getCode() == type){
            if(!"COMPANY".equals(biztype.toUpperCase())){
                throw new FunctionException(ErrorMessage.code("OTS-01-003-01-15-011"));
            }
        }
        OrganizationSDO parentOrg = organizationRepository.getOrganizationById(sdo.getParentId());
        //不能跳过一层创建组织
        if(!OrganizationTypeEnum.GROUP.getCode().equals(type) && Integer.valueOf(parentOrg.getType()) + 1 != Integer.valueOf(type)){
            throw new FunctionException(ErrorMessage.code("OTS-01-003-01-15-009"));
        };

        //组织类型分公司以下，关联公司取上级组织关联的公司
        if(OrganizationTypeEnum.SALES_ORGANIZATION.getCode() == type
                || OrganizationTypeEnum.SALES_CHANNEL.getCode() == type
                || OrganizationTypeEnum.SALES_DEPARTMENT.getCode() == type){

            sdo.setBizType(parentOrg.getBizType());
            sdo.setBizId(parentOrg.getBizId());
        }
        //剩下的各种不能为空判断交给基线
        return true;
    }

    public Boolean doCreateOrganizationRelation() {
        IdQueryRequest request = new IdQueryRequest();
        request.setId(relationSdo.getOrganiztionId());
        request.setType(relationSdo.getRelevanceType());
        request.setRelevanceId(relationSdo.getRelevanceId());
        request.setLimit(10);
        PageResult<List<OrganizationRelationSDO>> PageResult = organizationRelationRepository.getOrganizationRelationByOrgId(request);
        if(PageResult.getTotal()>0){
            throw new FunctionException(ErrorMessage.code("OTS-01-003-01-16-007"));
        }
        return organizationRelationRepository.addOrganizationRelation(relationSdo);
    }

    public Boolean doEnableOrganizationRelation(){
        relationSdo.setStatus(CommOperateTypeEnum.ENABLE.getCode());
        return organizationRelationRepository.editOrganizationRelationStatus(relationSdo);
    }

    public Boolean doDisableOrganizationRelation(){
        relationSdo.setStatus(CommOperateTypeEnum.DISABLE.getCode());
        return organizationRelationRepository.editOrganizationRelationStatus(relationSdo);
    }
}
